//
// Created by jmdvirus on 2020/4/28 下午2:49.
// Copyright (c) 2020 benewtech All rights reserved.
//     
//     ******          *             *--*
//       ||            *             |
//       ||  *--*--*---*  * *-********--*
//   *  //   *  *  *   *\ / ||  |  |    |
//    **    *  *  *---* *  *   *--* *--*
// 

#ifndef OPENGLFUNC_TESTCAMERASHOW_H
#define OPENGLFUNC_TESTCAMERASHOW_H

#include <QApplication>
#include <QLabel>
#include <QWidget>
#include <OpenGLQtWidget.h>
#include <OpenGLQtEasy.h>
#include "CMMediaVideo.h"
#include "cm_logprint.h"
#include "CameraCapture.h"
#include "funcs/cm_utils.h"

class TestCameraShow :public QWidget, public CMMediaVideoFunction {
    Q_OBJECT
public:
    TestCameraShow(QWidget *parent = NULL) :QWidget(parent)
    {
        setGeometry(0, 0, 480, 320);
        m_gl = new OpenGLQtWidget(this);
        m_gl->setGeometry(0, 0, 480, 320);
        m_gl->show();
        m_easy = NULL;
//        m_easy = new OpenGLQtEasy(480, 320, this);
//        m_easy->show();
//        m_videoWidth = 1280;
//        m_videoHeight = 720;
//2592_1936
//        m_videoWidth = 1920;
//        m_videoHeight = 1080;
        m_videoWidth = 2592;
        m_videoHeight = 1936;
    }
    ~TestCameraShow() {
        delete m_gl;
    }

    int init()
    {
        m_video = new CMMediaVideo();
        m_video->registerFunc(this);
        m_video->setNeedConvert(false);
        m_video->setSize(m_videoWidth, m_videoHeight);
        setName("TestCameraShow");
        int ret = m_video->open(0);
        if (ret < 0)
        {
            cmerror("Error open [%d]\n", ret);
            return ret;
        }
        cminfo("open success\n");

        m_file = fopen("/sdcard/tmp_x.yuv", "w");
        return 0;
    }

    int deinit()
    {
        cminfo("deinit\n");
        m_video->unregisterFunc(this);
        m_video->close();
        delete m_video;
        fclose(m_file);
    }

    int onFrame(const cv::Mat &frame, const cv::Mat &convertFrame) override
    {
        if (!frame.empty())
        {
//            size_t size = 640*360*1.5;
//            int ret = fwrite(frame.data, 1, size, m_file);
//            printf("frame set [%d]\n", ret);
            if (m_easy)
            {
                QImage img;
                cv::Mat toFrame;
                unsigned long long x = cm_gettime_milli();
                cv::cvtColor(frame, toFrame, cv::COLOR_YUV2BGR_I420);
                unsigned long long y = cm_gettime_milli();
                printf("------use time[ %llu]\n", y-x);
                m_capture.Mat2QImage(toFrame, img);
                m_easy->renderDisplay(img);
            }
            else
            {
//                m_gl->set_data(frame.data, m_videoWidth, m_videoHeight);
            }
        }
        else
        {
            cmwarn("no frame\n");
        }
        return 0;
    }

private:
    cv::Mat  m_frame;
    OpenGLQtWidget *m_gl;
    CMMediaVideo *m_video;
    OpenGLQtEasy   *m_easy;
    CameraCapture   m_capture;
    QLabel m_label;
    FILE *m_file;
    int m_videoWidth;
    int m_videoHeight;
};


#endif //OPENGLFUNC_TESTCAMERASHOW_H
